package com.accenture.portal.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.accenture.portal.model.Scheduling;
import com.accenture.portal.model.User;
import com.accenture.portal.service.SchedulingService;

/**
 * 
 * @author cuiying.cao
 *
 */
@Controller
public class SchedulingController {

	@Autowired
	@Resource(name="schedulingServiceImpl")
	private SchedulingService ss;
	
	/*
	 * 当前登陆身份下的scheduling信息
	 * */
//	@RequestMapping(value = "scheduling.action")
//	public String selectSchedulingByUserId(
//			HttpServletRequest request, HttpServletResponse response,
//			Model model, Integer user_id) {
//		List<Scheduling> list = ss.selectSchedulingByUserId(user_id);
//		model.addAttribute("scheduleList", list);
//		return "scheduling";
//	}
//	
	/*
	 * scheduling后台显示所有信息
	 * */
//	@RequestMapping(value = "schedulingBackGround.action")
//	public String findAll(@ModelAttribute("scheduling") Scheduling scheduling,
//			HttpServletRequest request, HttpServletResponse response,
//			Model model) {
//		List<Scheduling> list = ss.findAll();
//		model.addAttribute("listView", list);
//		int number = (int)request.getSession().getAttribute("count");
//		return "redirect:searchPage.action?number=" + number;
//	}
	
	/*
	 * 跳转到scheduling_add.jsp
	 * */
	@RequestMapping(value = "forwardToAddSchedulingPage.action", method = RequestMethod.GET)
	public String addScheduling(
			HttpServletRequest request, HttpServletResponse response,
			Model model, User user) {
		List<User> list = ss.allUsers();
		model.addAttribute("userList", list);
		return "scheduling_add";
	}
	
	/*
	 * scheduling_add.jsp保存
	 * */
	@RequestMapping(value = "addScheduling.action", method = RequestMethod.GET)
	public String addScheduling(@ModelAttribute("scheduling") Scheduling scheduling,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		ss.addScheduling(scheduling);
//		SimpleDateFormat formatter = new SimpleDateFormat("MMM dd,yyyy",Locale.ENGLISH);
//		String startDate = formatter.format(scheduling.getStartDate());
//		String endDate = formatter.format(scheduling.getEndDate());
//		String mycvResume = formatter.format(scheduling.getMycvResume());
//		System.out.println(startDate);
//		System.out.println(endDate);
//		System.out.println(mycvResume);
		int number = (int)request.getSession().getAttribute("count");
		return "redirect:searchPage.action?number=" + number;		
	}
	
	/*
	 * 删除scheduling信息
	 * */
	@RequestMapping(value = "deleteScheduling.action", method = RequestMethod.GET)
	public String deleteScheduling(@ModelAttribute("scheduling") Scheduling scheduling,
			HttpServletRequest request, HttpServletResponse response,
			Model model, Integer schedule_id) {
		ss.deleteScheduling(schedule_id);
		int number = (int)request.getSession().getAttribute("number");
		return "redirect:searchPage.action?number=" + number;		
	}
	
	/*
	 * 跳转到scheduling_edit.jsp
	 * 
	 * */
	@RequestMapping(value = "goToEditSchedulingPage.action", method = RequestMethod.GET)
	public String editScheduling(
			Model model, Integer schedule_id) {				
		Scheduling sche = ss.getSchedulingByScheduleId(schedule_id);
		model.addAttribute("scheduling", sche);		
		List<User> list = ss.allUsers();
		model.addAttribute("userList", list);
		return "scheduling_edit";	
	}
	 
	/*
	 * scheduling_edit.jsp保存
	 * */
	@RequestMapping(value = "editScheduling.action", method = RequestMethod.GET)
	public String editScheduling(@ModelAttribute("scheduling") Scheduling scheduling,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		ss.editScheduling(scheduling);
		int number = (int)request.getSession().getAttribute("number");
		return "redirect:searchPage.action?number=" + number;
	}
	
	/* 
     *分页  
     */  
    @RequestMapping(value="searchPage.action")  
    public String searchPage(@RequestParam("number") int number, HttpServletRequest request,Model model){
    	int page = 4;    	
    	
		if (number < 1) {
			number = 1;
		}
		
		int total = ss.total();
    	int count = total/page;
    	
    	if (total % page != 0) {
    		count++;
    	}  
    	if(count==0){ 
			count=1;//数据库没有数据时显示在总共1页
		}
    	if (number > count) {
    		number = count;
    	}
    	
    	
    	List<Scheduling> li = ss.searchPage((number-1)*page, page);
    	request.getSession().setAttribute("number", number);
    	model.addAttribute("listView", li);
    	request.getSession().setAttribute("count", count);  	
    	return "scheduling_background";
    }
}
